//
//  HSTopicDetailCell.m
//  HolyShit
//
//  Created by Robin on 2017/4/5.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSTopicDetailCell.h"
#import "NSString+Html.h"
#import "NSDate+MY_DateFormat.h"

@interface HSTopicDetailCell ()

@property (nonatomic, strong) UIImageView *iconImgV;
@property (nonatomic, strong) UILabel *nameLb;
@property (nonatomic, strong) UILabel *floorLb;
@property (nonatomic, strong) UILabel *contentLb;
@property (nonatomic, strong) UILabel *timeLb;
@property (nonatomic, strong) UILabel *likeLb;
@property (nonatomic, strong) UILabel *commentLb;
@property (nonatomic, strong) UIView *bottomV;

@end

@implementation HSTopicDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    self.backgroundColor = WHITE_COLOR;
    self.width = ScreenWidth;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.iconImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 35) imageType:MYImageTypeSmallHead];
    [self addSubview:self.iconImgV];
    [self.iconImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(15);
        make.width.height.mas_equalTo(35);
    }];
    
    self.nameLb = [UILabel labelWithFont:MYFont(Font30) color:HEXCOLOR(HSColorBlue)];
    [self addSubview:self.nameLb];
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.iconImgV);
        make.left.equalTo(self.iconImgV.mas_right).offset(5);
    }];
    
    self.floorLb = [UILabel labelWithFont:MYFont(Font28) color:HEXCOLOR(HSColorGray)];
    [self addSubview:self.floorLb];
    [self.floorLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.iconImgV);
        make.right.mas_offset(-15);
    }];
    
    self.contentLb = [UILabel labelWithFont:MYFont(Font34) color:HEXCOLOR(HSColorBlack)];
    self.contentLb.numberOfLines = 0;
    [self addSubview:self.contentLb];
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImgV);
        make.right.equalTo(self.floorLb);
        make.top.equalTo(self.iconImgV.mas_bottom).offset(15);
    }];
    
    self.timeLb =[UILabel labelWithFont:MYFont(Font26) color:HEXCOLOR(HSColorGray)];
    [self addSubview:self.timeLb];
    [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImgV);
        make.top.equalTo(self.contentLb.mas_bottom).offset(15);
    }];
    
    self.commentLb = [UILabel labelWithFont:MYFont(Font26) color:HEXCOLOR(HSColorRed)];
    [self addSubview:self.commentLb];
    [self.commentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.timeLb);
        make.right.equalTo(self.floorLb);
    }];
    self.commentLb.userInteractionEnabled = YES;
    @weakify(self);
    [self.commentLb bk_whenTapped:^{
        @strongify(self);
        if (self.replyBlock) {
            self.replyBlock(self.model.attributes.username);
        }
    }];
    
    self.likeLb = [UILabel labelWithFont:MYFont(Font26) color:HEXCOLOR(HSColorRed)];
    [self addSubview:self.likeLb];
    [self.likeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.timeLb);
        make.right.equalTo(self.commentLb.mas_left).offset(-20);
    }];
    
    self.bottomV = [UIView new];
    self.bottomV.backgroundColor = HEXCOLOR(HSColorBgLight);
    [self addSubview:self.bottomV];
    [self.bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(self.timeLb.mas_bottom).offset(15);
        make.height.mas_offset(10);
    }];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:cancle];
        UIAlertAction *copy = [UIAlertAction actionWithTitle:@"复制" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [UIPasteboard generalPasteboard].string = self.model.attributes.content;
            [UIWindow showSuccessMsg:@"复制成功"];
        }];
        [alert addAction:copy];
        UIAlertAction *warn = [UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [UIWindow showSuccessMsg:@"举报成功"];
        }];
        [alert addAction:warn];
        [[UIViewController my_topViewController] presentViewController:alert animated:YES completion:nil];
    }];
    [self addGestureRecognizer:longPress];
}

- (void)setModel:(HSForumListModel *)model
{
    _model = model;
    
    [self.iconImgV my_setImage:model.attributes.avatarUrl];
    self.nameLb.text = model.attributes.username ? : @"匿名用户";
    NSString *floor;
    switch (model.attributes.number) {
        case 1:
            floor = @"楼主";
            break;
        case 2:
            floor = @"沙发";
            break;
        case 3:
            floor = @"板凳";
            break;
        default:
            floor = [NSString stringWithFormat:@"%lu楼",model.attributes.number];
            break;
    }
    self.floorLb.text = floor;
    if (model.attributes.content.length < 1) {
        model.attributes.content = [model.attributes.contentHtml replaceTagAll];
    }
    self.contentLb.text = model.attributes.content;
    NSDate *date = [NSDate dateWithISOFormatString:model.attributes.time];
    self.timeLb.text = [date my_getDisplayTimeString];

    self.likeLb.text = [NSString stringWithFormat:@"%lu 赞",model.relationships.likes.count];
    self.commentLb.text = [NSString stringWithFormat:@"%lu 回复", model.relationships.mentionedBy.count];
    
    if (model.attributes.cellHeight < 1) {
        [self layoutSubviews];
        model.attributes.cellHeight = self.bottomV.bottom;
    }
}

@end
